<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="店铺名称" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="店铺地址" prop="address">
				<el-input v-model="form.address" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系人姓名" prop="uname">
				<el-input v-model="form.uname" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系人手机" prop="tel">
				<el-input v-model="form.tel" clearable type="number" maxlength="11"></el-input>
			</el-form-item>
			<el-form-item label="关联商家" prop="admin_id" >
			    <el-select
			        v-model="form.admin_id"
			        filterable
			        placeholder="请选择"
					style="width: 100%;"
			    >
			        <el-option
			            v-for="item in admin"
			            :key="item.id"
			            :label="item.name+' 【'+item.role_name+'】'"
			            :value="item.id"
			        />
			    </el-select>
			</el-form-item>
			<el-form-item label=" " prop="">
				   <el-link type="primary" :underline="false" @click="create_account">点我快捷创建商家</el-link>
			</el-form-item>
			<el-form-item label="开通时间" prop="kai_time">
				<el-date-picker
				      v-model="form.kai_time"
				      type="datetime"
				      placeholder="选择日期时间">
				    </el-date-picker>
			</el-form-item>
			<el-form-item label="到期时间" prop="end_time">
				<el-date-picker
				      v-model="form.end_time"
				      type="datetime"
				      placeholder="选择日期时间">
				    </el-date-picker>
			</el-form-item>
			<el-form-item label="启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>

		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				admin:[],
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					status:'1',
				},
				//验证规则
				rules: {

					name: [
						{required: true, message: '请输入角色名称'}
					],
					uname: [
						{required: true, message: '请输入姓名'}
					],
					admin_id: [
						{required: true, message: '请选择关联管理员'}
					],
					tel: [
						{required: true, message: '请输入手机号'}
					],
					kai_time: [
						{required: true, message: '请选择开通时间'}
					],
					end_time: [
						{required: true, message: '请选择结束时间'}
					]
				}
			}
		},
		mounted() {
			this.getAdmin()
		},
		methods: {
			async create_account(){
				if(this.form.tel=='' || this.form.uanme=='' || this.form.tel==null || this.form.uname==null){
					this.$alert('手机姓名不能为空', "提示", {type: 'error'})
					return
				}
				var arr = {
					'account':this.form.tel,
					'name':this.form.uname,
					'password':this.form.tel+'!@#',
					'act':'add',
					'group':2
				}
				var res = await this.$API.system.universal.handle.post(arr,'/auth/admin_save');
				if(res.code == 200){
					this.getAdmin()
					this.$message.success("创建成功")
				}else{
					this.isSaveing = false
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			async getAdmin(){
				var res = await this.$API.system.universal.list.get({'nopage':1},'/auth/admin');
				if(res.code == 200){
					this.admin = res.data
				}
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.universal.handle.post(this.form,'/system/store_save');
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.isSaveing = false
							this.$alert(res.message, "提示", {type: 'error'})
						}

					}
				})
			},

			//表单注入数据
			setData(data){
				  this.form = data
			}
		}
	}
</script>

<style>
</style>
